package com.nbicc.anon.catalog.controller;

import com.nbicc.catalog.po.Cat;
import com.nbicc.catalog.service.ICatalogService;
import com.nbicc.common.ResponseObj;
import com.nbicc.common.TreeNode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Api(value = "目录(匿名)" , description = "目录操作")
@RestController
@RequestMapping(value = "/anon/cat")
public class AnonCatalogController {

    @Resource(name = "CatalogServiceImpl")
    private ICatalogService catalogService;

    @ApiOperation(value = "按id查询目录" , notes = "单个目录信息")
    @GetMapping("/id/{id}")
    @ResponseBody
    public ResponseObj selectById(@PathVariable int id) throws Exception {
        Cat cat = catalogService.selectById(id);
        return ResponseObj.success().setResult(cat);
    }

    @ApiOperation(value = "按id查询所有子目录" , notes = "根目录id为0")
    @GetMapping("/pid/{pid}")
    @ResponseBody
    public ResponseObj listChildrenByPid(@PathVariable int pid){
        List<Cat> cats = catalogService.listChild(pid);
        return ResponseObj.success().setResult(cats);
    }

    @ApiOperation(value = "按id查询子目录(树状)" , notes = "根目录id为0")
    @GetMapping("/tree/pid/{pid}")
    @ResponseBody
    public ResponseObj listChildrenByPidInTree(@PathVariable int pid){
        List<TreeNode> treeNodes = catalogService.listChildrenByPidInTree(pid);
        return ResponseObj.success().setResult(treeNodes);
    }

    @ApiOperation(value = "查询新闻所在的目录")
    @GetMapping("/newsId/{newsId}")
    @ResponseBody
    public ResponseObj selectByNewsId(@PathVariable long newsId) {
        List<Cat> catList = catalogService.selectByNewsId(newsId);
        return ResponseObj.success().setResult(catList);
    }

}
